<script>


import {save, list, edit, deleteBatch, delAppointmentScheduler, suspend} from "@/api/appointmentScheduler";
import {findAll} from "@/api/department";
import {findAllDoctorNames} from "@/api/doctor";

export default {
  name: "AppointmentScheduler",
  data() {
    return {
      tableData: [],
      doctorId: null,
      departmentId: null,
      workDate: null,
      dayPart: null,
      status: null,
      dayParts: [{value: '0', name: '上午'}, {value: '1', name: '下午'}],
      statusList: [{value: '-1', name: '停诊'}, {value: '0', name: '停约'}, {value: '1', name: '可约'}],
      departmentList: [],
      doctorList: [],
      props: {
        expandTrigger: 'hover',
        value: 'id',
        label: 'name',
        children: 'secondClassDepartmentList'
      },
      currentPage: 1,
      pageSize: 5,
      total: 0,
      loading: true,
      multipleSelection: [],
      dialogFormVisible: false,
      form: {doctor: {user: {}, department: {}}},
      formLabelWidth: '150px',
      dialogFormVisible1: false,
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      let params = {
        doctorId: this.doctorId,
        departmentId: this.departmentId,
        workDate: this.workDate,
        dayPart: this.dayPart,
        status: this.status,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      }
      list(params).then(res => {
        this.tableData = res.data.records;
        console.log(this.tableData)
        this.loading = false;
        this.total = res.data.total;
      });
      this.findAllDoctorNames();
      this.findDepartmentList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    },
    reset() {
      this.doctorId = null,
          this.departmentId = null,
          this.workDate = null,
          this.dayPart = null,
          this.status = null,
          this.currentPage = 1;
      this.pageSize = 5;
      this.total = 0;
      this.load();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => a.id);
      console.log(this.multipleSelection);
    },
    findDepartmentList() {
      findAll().then(res => {
        this.departmentList = res.data;
      })
    },
    findAllDoctorNames() {
      findAllDoctorNames().then(res => {
        if (res.code === 200) {
          this.doctorList = res.data;
        } else {
          this.$message({
            message: "医生列表获取失败",
            type: 'error',
            center: true
          });
        }
      })
    },
    handleChange(departmentIds) {
      this.departmentId = departmentIds[1];
    },
    handleSave() {
      this.form = {doctor: {user: {}, department: {}}};
      this.dialogFormVisible = true;
    },
    save() {
      save(this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '排班创建成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible = false;
          this.form = {doctor: {user: {}, department: {}}};
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    handleEdit(row) {
      this.form = row;
      this.dialogFormVisible1 = true;
    },
    edit() {
      console.log(this.form)
      edit(this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '排班信息修改成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible1 = false;
          this.form = {doctor: {user: {}, department: {}}};
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    handleBatchRemove() {
      let ids = this.multipleSelection;
      deleteBatch(ids).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            center: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    suspend(appointmentSchedulerId) {
      this.$prompt('请输入停诊原因','停诊原因',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        inputType:'textarea'
      }).then(({value})=>{
        let params={
          appointmentSchedulerId:appointmentSchedulerId,
          suspendReason:value
        }
        suspend(params).then(res=>{
          if(res.code===200){
            this.$message({
              message: '停诊信息发布成功',
              type: 'success',
              center: true
            });
          }
        })
      });
    },
    handleDelete(id) {
      delAppointmentScheduler(id).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      });
    },
    view(suspendReason){
      this.$alert(suspendReason,"停诊原因",{
        confirmButtonText:'确定'
      })
    },
    headerBg({row, rowIndex}) {
      if (rowIndex == 0) {
        return 'headerBgClass';
      }
    }
  }
}
</script>

<template>
  <div>
    <div class="search">
      <el-select v-model="doctorId" filterable placeholder="请选择排班医生" clearable>
        <el-option v-for="item in doctorList" :key="item.doctorId"
                   :label="item.doctorName+'-'+item.departmentName" :value="item.doctorId">
        </el-option>
      </el-select>
      <el-date-picker type="date" v-model="workDate" format="yyyy年MM月dd日" value-format="yyyy-MM-dd"
                      placeholder="请选择排班日期" class="ml-5" clearable></el-date-picker>
      <el-cascader @change="handleChange" v-model="departmentId" filterable :options="departmentList" :props="props"
                   :show-all-levels="false" placeholder="请选择排班科室" class="ml-5" clearable></el-cascader>
      <el-select v-model="dayPart" filterable placeholder="请选择排班时段" class="ml-5" clearable>
        <el-option v-for="item in dayParts" :key="item.value" :label="item.name" :value="item.value"></el-option>
      </el-select>
      <el-select v-model="status" filterable placeholder="请选择排班状态" class="ml-5" clearable>
        <el-option v-for="item in statusList" :key="item.value" :label="item.name" :value="item.value"></el-option>
      </el-select>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <div style="margin:10px 0px" class="operation">
      <el-button type="primary" class="ml-5" icon="el-icon-circle-plus-outline" @click="handleSave">新增</el-button>
      <el-dialog :visible.sync="dialogFormVisible" title="新增排班信息" width="30%" center>
        <el-form :model="form" label-width="100px" style="width:340px">
          <el-form-item label="排班医生">
            <el-select v-model="form.doctorId" filterable placeholder="请选择排班医生" clearable>
              <el-option v-for="item in doctorList" :key="item.doctorId"
                         :label="item.doctorName+'-'+item.departmentName" :value="item.doctorId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排班日期">
            <el-date-picker type="date" v-model="form.workDate" format="yyyy年MM月dd日" value-format="yyyy-MM-dd"
                            placeholder="请选择排班日期" clearable></el-date-picker>
          </el-form-item>
          <el-form-item label="排班时段">
            <el-select v-model="form.dayPart" filterable placeholder="请选择排班时段" clearable>
              <el-option v-for="item in dayParts" :key="item.value" :label="item.name" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间">
            <el-time-picker v-model="form.startTime"
                            :picker-options="{selectableRange: ['09:00:00 - 09:30:00', '13:30:00 - 14:00:00']}"
                            value-format="HH:mm:ss" clearable></el-time-picker>
          </el-form-item>
          <el-form-item label="看诊地点">
            <el-input v-model="form.location" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="可预约数">
            <el-input-number v-model="form.reservedNumber" size="mini" controls-position="right" :min="1"
                             :max="20"></el-input-number>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="save">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除吗？"
          @confirm="handleBatchRemove"
          class="ml-5"
      >
        <el-button type="danger" class="ml-5" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
    </div>
    <div class="table">
      <el-table :data="tableData"
                border stripe
                v-loading="loading"
                max-height="550"
                :header-cell-class-name="headerBg"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(255,225,255, 0.1)"
                @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="40px" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="50px" align="center">
        </el-table-column>
        <el-table-column prop="doctor.user.userName" label="排班医生" width="70px" align="center">
        </el-table-column>
        <el-table-column prop="doctor.department.name" label="所属科室" width="90px" align="center">
        </el-table-column>
        <el-table-column prop="workDate" label="排班日期" width="90px" align="center">
        </el-table-column>
        <el-table-column label="排班时段" width="70px" align="center">
          <template slot-scope="scope">
            {{ scope.row.dayPart == '0' ? '上午' : '下午' }}
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="80px" align="center">
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="80px" align="center">
        </el-table-column>
        <el-table-column prop="location" label="看诊地点" width="98px" align="center"></el-table-column>
        <el-table-column prop="reservedNumber" label="可预约数" width="70px" align="center"></el-table-column>
        <el-table-column prop="availableNumber" label="剩余预约数" width="100px" align="center"></el-table-column>
        <el-table-column label="排班状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status=='1'" type="primary">可约</el-tag>
            <el-tag v-else-if="scope.row.status=='0'" type="warning">停约</el-tag>
            <el-tag v-else type="danger">停诊</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="340px" align="center">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-edit" style="margin-left: 5px;margin-bottom: 5px" v-if="scope.row.status!='-1'"
                       @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-dialog :visible.sync="dialogFormVisible1" title="编辑排班信息" width="30%" center>
              <el-form :model="form" label-width="100px" style="width:340px">
                <el-form-item label="排班医生">
                  <el-input v-model="form.doctor.user.userName" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="排班日期">
                  <el-input v-model="form.workDate" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="排班时段">
                  <el-select v-model="form.dayPart" disabled placeholder="请选择排班时段" clearable>
                    <el-option v-for="item in dayParts" :key="item.value" :label="item.name"
                               :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="开始时间">
                  <el-input v-model="form.startTime" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="结束时间">
                  <el-input v-model="form.endTime" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="看诊地点">
                  <el-input v-model="form.location" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="可预约数">
                  <el-input-number v-model="form.reservedNumber" size="mini" controls-position="right"
                                   :min="form.reservedNumber" :max="20"></el-input-number>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="edit">确 定</el-button>
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
              </div>
            </el-dialog>
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="handleDelete(scope.row.id)"
            >
              <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
            </el-popconfirm>
            <el-button type="warning" icon="el-icon-edit" style="margin-left: 5px" @click="suspend(scope.row.id)" v-if="scope.row.status !='-1'">
              停诊
            </el-button>
            <el-button type="info" icon="el-icon-view" style="margin-left: 5px"  v-if="scope.row.status =='-1'"  @click="view(scope.row.suspendReason)">
              停诊原因
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding:10px 0px">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<style scoped>
.headerBgClass {
  background-color: #cccccc !important;
}
</style>